/**
* 工厂模式
*/

#include <iostream>
#include <memory>

using namespace std;

// 产品类（抽象类，不能实例化）
class Product {
public:
    Product() = default;

    virtual void show() = 0;  //纯虚函数
    virtual ~Product() = default;
};

class productA : public Product {
public:
    productA() = default;

    void show() override { cout << "product A created!" << endl; };

    ~productA() override { cout << "product A deleted!" << endl; };
};

class productB : public Product {
public:
    productB() = default;

    void show() override { cout << "product B created!" << endl; };

    ~productB() override { cout << "product B deleted!" << endl; };
};

class SimpleFactory { // 工厂类
public:
    SimpleFactory() = default;

    Product *product(const string str) {
        if (str == "productA")
            return (new productA());
        if (str == "productB")
            return (new productB());
        return nullptr;
    };

    ~SimpleFactory() = default;
};

int main() {
    SimpleFactory obj; // 创建工厂
    // 创建产品
    std::unique_ptr<Product> proA(obj.product("productA"));
    proA->show(); // product A create!

    std::unique_ptr<Product> proB(obj.product("productB"));
    proB->show(); // product B create!

    return 0;
}
